package world.criver.h50909.function;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import world.criver.h50909.properties.proConfig;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Base64;
@Component
public class funSaveImage {
    @Autowired
    private proConfig proConfig;

    /**
     * 保存图片
     * @param image
     * @return
     */
    public long save(String image){
        try{
            long t = System.currentTimeMillis();
            File f = new File(proConfig.getLocal_path()+"material");
            if(!f.exists() || f.isFile())f.mkdirs();
            String fix=image.split(";")[0].split("/")[1];
            f=new File(proConfig.getLocal_path() + "material/" + t+"." + fix);
            BufferedImage im= ImageIO.read(new java.io.ByteArrayInputStream(Base64.getDecoder().decode(image.split(",")[1])));
            return ImageIO.write(im,fix,f)?t:-1;
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}